<?php
/*
    author:lijj
    2016-02-17
    开发点 ： 1、用了think框架
              2、加了表情
*/

class WeiXinAction extends Action{      
    public function dowx(){
        define("TOKEN", "weixin");
        define("WEB_SITE","http://testet.sinaapp.com/index.php");
        define("WEI_PIC","http://testet.sinaapp.com/Public/images/weixin");
        define("APP_MINGZI", "概率论与数理统计教学平台");
        $wechatObj = new wechatCallbackapiTest();
        if (isset($_GET['echostr'])) {
            $wechatObj->valid();
        }
        else{
            $wechatObj->responseMsg();
        }
    }
    // 检测是否注册
    public function checkRegister($openId){
        $STU = D('Student/StudentInfo');
        $TEA = D('Teacher/TeacherInfo');
        if($STU->where('openId="'.$openId.'"')->find() || $TEA->where('openId="'.$openId.'"')->find())
            return ture;
        else
            return false;
    }
    // 获得姓名 班级 班级
    public function getName($openId){
        $STU = D('Student/StudentInfo');
        return $STU->getName($openId);
    }
    public function getClass($openId){
        $STU = D('Student/StudentInfo');
        return $STU->getClass($openId);
    }
    public function getNumber($openId){
        return D('Student/StudentInfo')->where('openId="'.$openId.'"')->getField('number');
    }
   
    //将用户发送的消息存到数据库
    public function recordMessage($openid,$messContent){
        $MESSREC = D('Student/WeixinMessageRecord');
        $name = $this->getName($openid);
        $class = $this->getClass($openid);
        $number = $this->getNumber($openid);
        $message = array(
                'openId' => $openid.'',       //加上后面的空字符串表示转型成string
                'name'   => $name,
                'class'  => $class,
                'number' => $number,
                'messageContent' => "".$messContent,           //同openID
                'time'   => date('Y-m-d H:i:s') );
        if($this->checkRegister($openid))
            $MESSREC->add($message);
    }

    // 计算分数
    // 关注并且注册        5
        //微信端发送一条消息   0.5
        //自由练习做的题目     2
        // 对的题目            在做的基础上再加3分
        // 试题评论次数        1.5
        // 留言次数            1.5
        // 最终成绩按照  分数排名+然后正态分布(满分20分)
    public function getMark($openId){
        /*========定义一些变量==========*/
        $MESSREC     = D('Student/WeixinMessageRecord');
        $COM_COMMENT =  D('Student/CommunityComment');
        $COM_REPLY   = D('Student/CommunityReply');
        $RAN_COMMENT = D('Student/RandomComment');
        $RAN_REPLY   = D('Student/RandomReply');
        $RAN_RECORD  = M('random_answer_record');

        $weixinMessageNum = $MESSREC->where('openId="'.$openId.'"')->count();   //微信后台回复的数量
        $comCommentNum    = $COM_COMMENT->where('openId="'.$openId.'"')->count();    //社区留言评论
        $comReplyNum      = $COM_REPLY->where('openId="'.$openId.'"')->count();         //社区留言回复
        $ranCommentNum    = $RAN_COMMENT->where('openId="'.$openId.'"')->count(); //自由练习评论
        $ranReplyNum      = $RAN_REPLY->where('openId="'.$openId.'"')->count();   //自由练习回复
        $doRanNum         = $RAN_RECORD->where('openId="'.$openId.'"')->count();   //自由练习做的题目的个数
        $doRanRightNum    = $RAN_RECORD->where('openId="'.$openId.'" AND answerResult="RIGHT"')->count();   //自由练习对的个数
        $registerNum      = $this->checkRegister($openId)? 1 : 0 ;   //是否注册
        $mark             = $registerNum*5 + $weixinMessageNum*0.5 + $doRanNum*2 + $doRanRightNum*3 + ($comCommentNum+$comReplyNum+$ranReplyNum+$ranCommentNum)*3;

        return $mark;
    }
    // 班级人数
    public function getClassNum($openId){
        $STU = D('Student/StudentInfo');
        $class = $STU->getClass($openId);
        return $STU->where('class="'.$class.'"')->count();  //获得总人数
    }
    // 班级排名
    public function getClassRank($openId,$type=flase){  //type对应输出不同的结果
        $STU = D('Student/StudentInfo');
        $class = $STU->getClass($openId);
        $r = new SaeRank();
        $num = $this->getClassNum($openId);  //获得总人数
        $r->create("list", $num); //创建一个榜单。
        $stuInfo = $STU->where('class="'.$class.'"')->select();
        foreach ($stuInfo as $key => $value) {
            $r->set("list", $stuInfo[$key]['openId'], $this->getMark($stuInfo[$key]['openId'])); //设置值    
        }
        $ret = $r->getList("list", true); //获得排行榜
        $retPerson = $r->getRank("list", $openId); //获得某个键的排名,注意是从0开始
        $r->clear("list"); //清空排行榜
        if(!$type)
            return $retPerson+1; //第一名是0
        return $ret;

    }
    // 平台总人数
    public function getAllNum($openId){
        $STU = D('Student/StudentInfo');
        return $STU->count();  //获得总人数
    }
    public function getAllRank($openId, $type=flase) {
        $STU = D('Student/StudentInfo');
        $r = new SaeRank();
        $num = $STU->count();  //获得总人数
        $r->create("list", $num); //创建一个榜单。
        $stuInfo = $STU->select();
        foreach ($stuInfo as $key => $value) {
            $r->set("list", $stuInfo[$key]['openId'], $this->getMark($stuInfo[$key]['openId'])); //设置值    
        }
        $ret = $r->getList("list", true); //获得排行榜
        $retPerson = $r->getRank("list", $openId); //获得某个键的排名,注意是从0开始
        $r->clear("list"); //清空排行榜
        if(!$type)
            return $retPerson+1; //第一名是0
        else
            return $ret;
    }
}
class wechatCallbackapiTest extends WeiXinAction{
    //验证消息
    public function valid(){
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
    //检查签名
    private function checkSignature(){
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }
    //菜单
    public function menu($object){
        $openId = $object->FromUserName;
        if($this->checkRegister($openId)){   //注册了
            switch ($object->Content){
                case "1":
                    $content = array();
                    $content[] = array("Title"=>"注册信息", "Description"=>"", "PicUrl"=>WEI_PIC."/register.jpg", "Url" => WEB_SITE."/Index/register/openId/$openId");
                    break;
                case "2":
                    $content = array();
                    $content[] = array("Title"=>"信息修改", "Description"=>"", "PicUrl"=>WEI_PIC."/changeInfo.jpg", "Url" => WEB_SITE."/Index/changeInfo");
                    break;
                case "3":
                    $content = array();
                    $content[] = array("Title"=>"随机练习", "Description"=>"", "PicUrl"=>WEI_PIC."/random.jpg", "Url" => WEB_SITE."/Random/index");
                    break;
                case "4":
                    $content = array();
                    $content[] = array("Title"=>"社区留言", "Description"=>"", "PicUrl"=>WEI_PIC."/community.jpg", "Url" => WEB_SITE."/Community/index");
                    $content[] = array("Title"=>"社区留言", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Community/index");
                    break;
                case "5":
                    $content  = "姓名：".$this->getName($openId)."\n";
                    $content .= "班级：".$this->getClass($openId)."\n";
                    $content .= "学号：".$this->getNumber($openId)."\n";
                    $content .= "成绩：".$this->getMark($openId)."\n";
                    $content .= "班级排名：".$this->getClassRank($openId,false)."\n";
                    $content .= "平台排名：".$this->getAllRank($openId,false)."\n";
                    $content .= "ps:回复51查看班级前5名成绩，回复52查看平台前5名成绩";
                    break;
                case "51":
                    $classRank = $this->getClassRank($openId,true);
                    $content = array();
                    $content[] = array("Title"=>$this->getClass($openId)."班前5名", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"");
                    for ($i=0; $i < 5; $i++) { 
                        $id = key($classRank[$i]);
                        $content[] = array("Title"=>$this->getName($id)."同学        ".$this->getMark($id)."分", "Description"=>"", "PicUrl"=>"", "Url" =>"");    
                    }
                    break;
                case "52":
                    $allRank = $this->getAllRank($openId,true);
                    $content = array();
                    $content[] = array("Title"=>"概率论与数理统计教学平台成绩   前5名", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"");
                    for ($i=0; $i < 5; $i++) { 
                        $id = key($allRank[$i]);
                        $content[] = array("Title"=>$this->getClass($id)."班    ".$this->getName($id)."同学        ".$this->getMark($id)."分", "Description"=>"", "PicUrl"=>"", "Url" =>"");    
                    }
                    break;
                case "6":
                    $content = array();
                    $content[] = array("Title"=>"资料下载", "Description"=>"", "PicUrl"=>WEI_PIC."/fileDownload.jpg", "Url" => WEB_SITE."/Admin/Upload/fileDownload" );
                    break;
                case "7":
                    $content = array();
                    $content[] = array("Title"=>"随堂测试", "Description"=>"", "PicUrl"=>WEI_PIC."/classtest.jpg", "Url" => WEB_SITE."/ClassTest/classTest" );
                    break;
                case "000000":
                    $content = array();
                    $content[] = array("Title"=>"授权登录", "Description"=>"", "PicUrl"=>WEI_PIC."/authenticate.jpg", "Url" => WEB_SITE."/Index/authenticate/openId/$openId" );
                    break;
                default:
                    $content = "发送？查看平台使用帮助";
                    break;
            }
        }else{  //没有注册
            switch ($object->Content){
                case "1":
                    $content = array();
                    $content[] = array("Title"=>"注册信息", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Index/register/openId/$object->FromUserName");
                    break;
                case "52":
                    $allRank = $this->getAllRank($openId,true);
                    $content = array();
                    $content[] = array("Title"=>"概率论与数理统计教学平台成绩   前5名", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"");
                    for ($i=0; $i < 5; $i++) { 
                        $id = key($allRank[$i]);
                        $content[] = array("Title"=>$this->getClass($id)."班    ".$this->getName($id)."同学        ".$this->getMark($id)."分", "Description"=>"", "PicUrl"=>"", "Url" =>"");    
                    }
                    break;
                default:
                    $content = array();
                    $content[] = array("Title"=>"你还没有注册，请发送1注册", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"");
                    $content[] = array("Title"=>"发送？查看平台使用帮助", "Description"=>"", "PicUrl"=>"");
                    break;
                }
        }
        $this->recordMessage($object->FromUserName,$object->Content);
        if($object->Content == "?" || $object->Content == "？"){
            $content = array();
            $content[] = array("Title"=>"欢迎你关注概率论与数理统计教学互动平台", "Description"=>"", "PicUrl"=>WEI_PIC."/help.jpg", "Url" =>"");
            $content[] = array("Title"=>"发送1 : 注册信息", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Index/register/openId/$openId");
            $content[] = array("Title"=>"发送2 : 修改信息", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Index/changeInfo");
            $content[] = array("Title"=>"发送3 : 随机练习", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Random/index");
            $content[] = array("Title"=>"发送4 : 平台互动", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Community/index");
            $content[] = array("Title"=>"发送5 : 查看成绩", "Description"=>"", "PicUrl"=>"", "Url" =>"");
            $content[] = array("Title"=>"发送6 : 资料下载", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Admin/Upload/fileDownload");
            $content[] = array("Title"=>"发送? : 查看帮助", "Description"=>"", "PicUrl"=>"", "Url" =>"");
        }
        if(is_array($content)){
            if (isset($content[0]['PicUrl'])){
                $result = $this->transmitNews($object, $content);
            }else if (isset($content['MusicUrl'])){
                $result = $this->transmitMusic($object, $content);
            }
        }else{
            $result = $this->transmitText($object, $content);
        }
        return $result;
    }

    //响应消息
    public function responseMsg(){
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);
            switch ($RX_TYPE){
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "image":
                    $result = $this->receiveImage($postObj);
                    break;
                case "location":
                    $result = $this->receiveLocation($postObj);
                    break;
                case "voice":
                    $result = $this->receiveVoice($postObj);
                    break;
                case "video":
                    $result = $this->receiveVideo($postObj);
                    break;
                case "link":
                    $result = $this->receiveLink($postObj);
                    break;
                default:
                    $result = "unknown msg type: ".$RX_TYPE;
                    break;
            }
            $this->logger("T ".$result);
            echo $result;
        }else{
            echo "string"; "";
            exit;
        }
    }

    

    //用户关注
    private function attent($object){
        $contentStr = array();
        $contentStr[] = array("Title"=>"欢迎你关注概率论与数理统计教学互动平台", "Description"=>"", "PicUrl"=>WEI_PIC."/welcome.jpg", "Url" =>"");
        $content = array();
            $content[] = array("Title"=>"欢迎你关注概率论与数理统计教学互动平台", "Description"=>"", "PicUrl"=>WEI_PIC."/help.jpg", "Url" =>"");
            $content[] = array("Title"=>"发送1 : 注册信息", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Index/register/openId/$openId");
            $content[] = array("Title"=>"发送2 : 修改信息", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Index/changeInfo");
            $content[] = array("Title"=>"发送3 : 随机练习", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Random/index");
            $content[] = array("Title"=>"发送4 : 平台互动", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Community/index");
            $content[] = array("Title"=>"发送5 : 查看成绩", "Description"=>"", "PicUrl"=>"", "Url" =>"");
            $content[] = array("Title"=>"发送6 : 资料下载", "Description"=>"", "PicUrl"=>"", "Url" => WEB_SITE."/Admin/Upload/fileDownload");
            $content[] = array("Title"=>"发送? : 查看帮助", "Description"=>"", "PicUrl"=>"", "Url" =>"");
        return $contentStr;
    }

    //接收事件消息
    private function receiveEvent($object){
        $openId = $object->FromUserName;

        $content = "";
        switch ($object->Event){
            case "subscribe":
                $contentStr = $this->attent($object);
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
            case "SCAN":
                $content = "扫描场景 ".$object->EventKey;
                break;
            case "CLICK":
                if($this->checkRegister($openId)){
                    switch ($object->EventKey){
                        case "register":
                            $content = "这是一个注册界面,请在学生端获取该链接";
                            break;
                        case "myInfo":
                            $content = "这是一个查看个人信息界面,请在学生端获取该链接";
                            break;
                        case "ramdon":
                            $content = "这是一个查看自由练习信息界面,请在学生端获取该链接";
                            break;
                        case "tea_register":
                            $contentStr[] = array("Title"=>"老师端信息注册", "Description"=>"老师端注册个人信息", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Teacher/Index/register/openId/$object->FromUserName");
                            break;
                        case "tea_info":
                            $contentStr[] = array("Title"=>"老师端信息修改", "Description"=>"老师端修改个人信息", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Teacher/Index/changeInfo");
                            break;
                        case "tea_class":
                            $contentStr[] = array("Title"=>"我的课程", "Description"=>"老师端修改个人信息", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Teacher/Index/myClass");
                            break;
                        case "tea_classtest":
                            $contentStr[] = array("Title"=>"随堂测试", "Description"=>"老师端修改个人信息", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Teacher/ClassTest/index");
                            break;
                        case "tea_random":
                            $contentStr[] = array("Title"=>"随机练习", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Random/index/openid/$object->FromUserName");
                            break;
                        default:
                            $content = "点击菜单：".$object->EventKey;
                            break;
                    }
                }else{
                    switch ($object->EventKey){
                        case "tea_register":
                            $contentStr[] = array("Title"=>"老师端信息注册", "Description"=>"老师端注册个人信息", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" => WEB_SITE."/Teacher/Index/register/openId/$object->FromUserName");
                            break;
                        default:
                            $content = array();
                            $content[] = array("Title"=>"你还没有注册，请点击此链接注册", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"");
                            $content[] = array("Title"=>"发送？查看平台使用帮助", "Description"=>"", "PicUrl"=>"");
                            break;
                        }
                }
                break;
            case "LOCATION":
                $content = "上传位置：纬度 ".$object->Latitude.";经度 ".$object->Longitude;
                break;
            case "VIEW":
                $content = "跳转链接 ".$object->EventKey;
                break;
            default:
                $content = "receive a new event: ".$object->Event;
                break;
        }
        if(is_array($contentStr)){
            $resultStr = $this->transmitNews($object, $contentStr);
        }else{
            $resultStr = $this->transmitText($object, $content);
        }
        return $resultStr;
    }
    //接收文本消息
    private function receiveText($object){
        return $this->menu($object);
    }
    //接收图片消息
    private function receiveImage($object){
        $content = array("MediaId"=>$object->MediaId);
        $result = $this->transmitImage($object, $content);
        return $result;
    }
    //接收位置消息
    private function receiveLocation($object){
        $content = "你发送的是位置，纬度为：".$object->Location_X."；经度为：".$object->Location_Y."；缩放级别为：".$object->Scale."；位置为：".$object->Label;
        $result = $this->transmitText($object, $content);
        return $result;
    }
    //接收语音消息
    private function receiveVoice($object){
        if (isset($object->Recognition) && !empty($object->Recognition)){
            $content = "你刚才说的是：".$object->Recognition;
            $result = $this->transmitText($object, $content);
        }else{
            $content = array("MediaId"=>$object->MediaId);
            $result = $this->transmitVoice($object, $content);
        }
        return $result;
    }
    //接收视频消息
    private function receiveVideo($object){
        $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
        $result = $this->transmitVideo($object, $content);
        return $result;
    }
    //接收链接消息
    private function receiveLink($object){
        $content = "你发送的是链接，标题为：".$object->Title."；内容为：".$object->Description."；链接地址为：".$object->Url;
        $result = $this->transmitText($object, $content);
        return $result;
    }
    //回复文本消息
    private function transmitText($object, $content){
        $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[%s]]></Content>
        </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }
    //回复图片消息
    private function transmitImage($object, $imageArray)
    {
        $itemTpl = "<Image>
            <MediaId><![CDATA[%s]]></MediaId>
        </Image>";
        $item_str = sprintf($itemTpl, $imageArray['MediaId']);
        $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        $item_str
        </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    //回复语音消息
    private function transmitVoice($object, $voiceArray)
    {
        $itemTpl = "<Voice>
            <MediaId><![CDATA[%s]]></MediaId>
        </Voice>";
        $item_str = sprintf($itemTpl, $voiceArray['MediaId']);
        $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[voice]]></MsgType>
        $item_str
        </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    //回复视频消息
    private function transmitVideo($object, $videoArray)
    {
        $itemTpl = "<Video>
            <MediaId><![CDATA[%s]]></MediaId>
            <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
        </Video>";
        $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);
        $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[video]]></MsgType>
        $item_str
        </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    //回复图文消息
    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = "<item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
        </item>";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $newsTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[news]]></MsgType>
        <Content><![CDATA[]]></Content>
        <ArticleCount>%s</ArticleCount>
        <Articles>
        $item_str</Articles>
        </xml>";
        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }
    //回复音乐消息
    private function transmitMusic($object, $musicArray)
    {
        $itemTpl = "<Music>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <MusicUrl><![CDATA[%s]]></MusicUrl>
            <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
        </Music>";
        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
        $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[music]]></MsgType>
        $item_str
        </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    //日志记录
    private function logger($log_content)
    {
        if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
            sae_set_display_errors(false);
            sae_debug($log_content);
            sae_set_display_errors(true);
        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
            $max_size = 10000;
            $log_filename = "log.xml";
            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
            file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
        }
    }
}
